<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

require_once 'getyouridx/idxsearch.php';

/**
 * Example system plugin
 */
class plgContentGetYourIDX extends JPlugin
{
	/**
	 * Constructor
	 *
	 * For php4 compatibility we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @access	protected
	 * @param	object	$subject The object to observe
	 * @param 	array   $config  An array that holds the plugin configuration
	 * @since	1.0
	 */
	function plgContentGetYourIDX( &$subject, $config )
	{
		parent::__construct( $subject, $config );
	}

	function onPrepareContent( &$article, &$params, $limitstart )
    {
        
        // Add required Js and CSS files
        $document =& JFactory::getDocument();
        $document->addScript('http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js');
        $document->addScript('/plugins/content/getyouridx/js/jquery.prettyPhoto.js');
        $document->addStyleSheet('/plugins/content/getyouridx/css/getyouridx.css');
        $document->addStyleSheet('/plugins/content/getyouridx/css/prettyPhoto.css');
        
        //if ( JString::strpos( $row->text, 'getyouridx' ) === false ) {
        //    return true;
        //}
        $regex = '/{getyouridx\s*.*?}/i';
        
        if ( !$this->params->get( 'apikey', false ) || !$this->params->get( 'gapikey', false ) ) {
            $row->text = preg_replace( $regex, 'The GetYourIDX.com Plugin has not configured', $article->text );
            return true;
        }
        
        ob_start();
        if (isset($_GET['getyouridx_postback'])) {
            if (isset($_GET['mlsnumber'])) {
                $result = $this->getyouridx_get($_GET['mlsnumber']);
                include 'getyouridx/results_single.php';
            } else {
                $results = $this->getyouridx_search();
                include 'getyouridx/results_list.php';
            }
        }
        include 'getyouridx/form_large.php';
        $return = ob_get_contents();
        ob_end_clean();
        
        $article->text = preg_replace( $regex, $return, $article->text );
        
        return $return;
        
    }
    
    /**
     *
     */
    function getyouridx_get($mlsid) {
        
        $idx_search = new IDXSearch($this->params->get( 'apikey', '' ));
        
        $mls_details = split(':', $mlsid);
        $idx_search->add_filter(new IDXFilter_MLSNumber($mls_details[1]));
        $idx_search->add_filter(new IDXFilter_Format('xml'));
        $idx_search->add_filter(new IDXFilter_In('mls_id', array($mls_details[0])));
    
        return $idx_search->result();
        
    }
    
    
    /**
     *
     */
    function getyouridx_search() {
        
        $idx_search = new IDXSearch($this->params->get( 'apikey', '' ));
        
        // Property Type
        //
        if (isset($_GET['idx_propertytype']) & $_GET['idx_propertytype'] > 0) {
            $idx_search->add_filter(new IDXFilter_Equals('type', $_GET['idx_propertytype']));
        }
        
        // Price High to Low Handler
        //
        if (isset($_GET['idx_pricerange_low']) && isset($_GET['idx_pricerange_high']) && $_GET['idx_pricerange_low'] > 0 && $_GET['idx_pricerange_high'] > 0) {
            $idx_search->add_filter(new IDXFilter_Range('asking_price', $_GET['idx_pricerange_low'], $_GET['idx_pricerange_high']));
            
        } elseif (isset($_GET['idx_pricerange_low']) && isset($_GET['idx_pricerange_high']) && $_GET['idx_pricerange_low'] > 0 && $_GET['idx_pricerange_high'] = 0) {
            $idx_search->add_filter(new IDXFilter_GreaterThan('asking_price', $_GET['idx_pricerange_low']));
            
        } elseif (isset($_GET['idx_pricerange_low']) && isset($_GET['idx_pricerange_high']) && $_GET['idx_pricerange_low'] = 0 && $_GET['idx_pricerange_high'] > 0) {
            $idx_search->add_filter(new IDXFilter_LessThan('asking_price', $_GET['idx_pricerange_low']));
            
        }
        
        // Bedrooms
        //
        if (isset($_GET['idx_bedrooms']) && $_GET['idx_bedrooms'] > 0 && $_GET['idx_bedrooms'] < 4) {
            $idx_search->add_filter(new IDXFilter_Equals('bedrooms', $_GET['idx_bedrooms']));
            
        } else if (isset($_GET['idx_bedrooms']) && $_GET['idx_bedrooms'] == 4) {
            $idx_search->add_filter(new IDXFilter_GreaterThan('bedrooms', $_GET['idx_bedrooms']));
            
        }
        
        // Bathrooms
        //
        if (isset($_GET['idx_bathrooms']) && $_GET['idx_bathrooms'] > 0 && $_GET['idx_bathrooms'] < 4) {
            $idx_search->add_filter(new IDXFilter_Equals('bathrooms', $_GET['idx_bathrooms']));
            
        } else if (isset($_GET['idx_bathrooms']) && $_GET['idx_bathrooms'] == 4) {
            $idx_search->add_filter(new IDXFilter_GreaterThan('bathrooms', $_GET['idx_bathrooms']));
            
        }
        
        // Limit the results
        //
        if (isset($_GET['idx_results'])) {
            $idx_search->add_filter(new IDXFilter_Limit($_GET['idx_results']));
        }
    
        // Short Sale
        //
        if (isset($_GET['idx_shortsale'])) {
            $idx_search->add_filter(new IDXFilter_Equals('short_sale', 'True'));
        }
        
        // Paging
        //
        if (isset($_GET['offset'])) {
            $idx_search->add_filter(new IDXFilter_Offset($_GET['offset']));
        }
        
        // Sort
        //
        #if (isset($_GET['idx_sort'])) {
        #    $idx_search->add_filter(new IDXFilter_Sort($_GET['idx_sort']));
        #}
        
        // Add MLS
        //
        $idx_search->add_filter(new IDXFilter_In('mls_id', array('TSMLS')));
        $idx_search->add_filter(new IDXFilter_Format('xml'));
        
        // Return the result
        return $idx_search->result();
        
    }
}